package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//1,GetMapping("/menu/")
//1.1.1GetMapping("/menu/{id}")
//1.1.2GetMapping("/menu/{id}")
//PutMapping
//PostMapping
//DeleteMapping("/menu/{id}")

//2参数
//@PathVariable 表示该数据来自前端url
//@RequestBody  修饰方法参数 接收json数据({id:10,name:"",...})
//@RequestParam 接收("/?id=10&")


//3响应什么数据(JsonResult)  RestResult JsonResult ResponseResult


//4异常处理
//1,局部异常处理 Controller每个方法内部都定义异常类  try()Catch(){}  优先级最高
//2,Controller类的内部  Controller类的内部定义专门的异常处理方法  @ExceptionHandler
//3, Controller类的外部定义全局异常处理类(推荐)  @RestControllerAdvice

//5,控制Controller访问时间HandlerIntercepter


@RequestMapping("/menu/")
@RestController
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping
    public JsonResult doFindMenus(){

        return new JsonResult(sysMenuService.findMenus());
        //sysMenuService.findMenus()  --> Object data;
        //封装异常信息

        //        try {
//            return new JsonResult(sysMenuService.findMenus());
//        }catch (Exception e){
//            return new JsonResult(e);
//        }

    }
    @GetMapping("treeNodes/")
    public JsonResult dofindMenuTreeNodes(){
        return new JsonResult(sysMenuService.findMenuTreeNodes());
    }



    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable   Integer id){
        return new JsonResult(sysMenuService.findById(id));
    }
    //修改,更新update
    @PutMapping
    public JsonResult doUpdateMenu(@RequestBody SysMenu menu){
        sysMenuService.updateMenu(menu);
       return new JsonResult("Update ok!");
    }


    //新增
    @PostMapping
    public JsonResult doSaveMenu(@RequestBody SysMenu menu){
        sysMenuService.saveMenu(menu);
        return new JsonResult("Insert ok!");
    }

}
